﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;

using System.Text;
using System.Windows.Forms;
using Common.Utilities;
using Common.Utilities.Test;
using PdfLib;
using PdfLib.TextScraping;

namespace PdfLib.Test.Layouts
{
    [ManualTestCase("Table column layout", "Layout")]
    public partial class TestCase_ColumnLayout : UserControl
    {
        public TestCase_ColumnLayout()
        {
            InitializeComponent();
        }

        private void btn_Open_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "PDF files(*.pdf)|*.pdf|All files(*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txt_PdfPath.Text = dlg.FileName;
                int pageCount = PdfPropertyReader.GetPageCount(this.txt_PdfPath.Text);
                this.num_Page.Value = 1;
                this.num_Page.Maximum = pageCount;

                this.OpenPage(this.txt_PdfPath.Text, (int)this.num_Page.Value);
            }
        }

        private void btn_Open2_Click(object sender, EventArgs e)
        {
            this.OpenPage(this.txt_PdfPath.Text, (int)this.num_Page.Value);
        }

        private void OpenPage(string pdfFilePath, int pageNum)
        {
            this.dv_ColHeaderLayout.Rows.Clear();
            this.pic_ColLayout.Image = null;

            
            try
            {
                Image pageImg = PdfTextReader.GetPageTextLayout(pdfFilePath, pageNum);
                this.pic_PDF.Image = pageImg;
                TableColumnDetectorInstruction colDetectSettings = this.GetColDetectSettings();
                Dictionary<string, RectangleF> colHeaderLayouts =
                    TableColumnDetector.DetectTableColumnLayouts(
                        pdfFilePath, pageNum, colDetectSettings);

                this.dv_ColHeaderLayout.Rows.Clear();
                if(colHeaderLayouts !=null && colHeaderLayouts.Count>0)
                {
                    foreach (string colHeader in colHeaderLayouts.Keys)
                    {
                        DataGridViewRow dr = new DataGridViewRow();
                        DataGridViewTextBoxCell cellHeader = new DataGridViewTextBoxCell();
                        cellHeader.Value = colHeader;
                        dr.Cells.Add(cellHeader);
                        DataGridViewTextBoxCell cellX=new DataGridViewTextBoxCell();
                        cellX.Value = colHeaderLayouts[colHeader].X;
                        dr.Cells.Add(cellX);
                        DataGridViewTextBoxCell cellY = new DataGridViewTextBoxCell();
                        cellY.Value = colHeaderLayouts[colHeader].Y;
                        dr.Cells.Add(cellY);
                        DataGridViewTextBoxCell cellW = new DataGridViewTextBoxCell();
                        cellW.Value = colHeaderLayouts[colHeader].Width;
                        dr.Cells.Add(cellW);
                        DataGridViewTextBoxCell cellH = new DataGridViewTextBoxCell();
                        cellH.Value = colHeaderLayouts[colHeader].Height;
                        dr.Cells.Add(cellH);
                        this.dv_ColHeaderLayout.Rows.Add(dr);
                    }
                }

                Image img = PdfTextReader.GetPageTextLayout(pdfFilePath, pageNum);
                Graphics g = Graphics.FromImage(img);
                if(colHeaderLayouts !=null && colHeaderLayouts.Count>0)
                {
                    Color highlighter = Color.FromArgb(125, Color.Red);
                    foreach(string colHeader in colHeaderLayouts.Keys)
                    {
                        RectangleF rect = colHeaderLayouts[colHeader];
                        g.DrawRectangle(new Pen(highlighter, 1),
                                        (int) rect.X,
                                        (int) rect.Y,
                                        (int) rect.Width,
                                        (int) rect.Height);
                    }
                }
                this.pic_ColLayout.Image = img;
                g.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }


        #region settings
        private TableColumnDetectorInstruction GetColDetectSettings()
        {
            TableColumnDetectorInstruction colDetectorSettings = new TableColumnDetectorInstruction();
            colDetectorSettings.AdjustColumnHeaderWidthFromTableRowCount = (int) this.num_RowCountForColWidthMerge.Value;
            colDetectorSettings.MatchScoreThreshold = (double) this.num_MinScore.Value;
            colDetectorSettings.ColumnHeaderSlidingWindwSize = (int) this.num_ColHeaderWindowSize.Value;
            colDetectorSettings.ColumnHeaderSlidingWindowStep = (int) this.num_SlidingWindowStep.Value;
            colDetectorSettings.ColumnLayoutDetectionType = ColumnBorderDetectorType.PdfImageRectangle;
            if(this.cbo_ColLayoutDetectMethods.SelectedIndex==0)
                colDetectorSettings.ColumnLayoutDetectionType = ColumnBorderDetectorType.SlidingWindow;
            else if (this.cbo_ColLayoutDetectMethods.SelectedIndex ==1)
                colDetectorSettings.ColumnLayoutDetectionType = ColumnBorderDetectorType.PdfPathRectangle;

            colDetectorSettings.ColumnHeadersInFile = new List<string>();
            foreach (DataGridViewRow dr in this.dv_ColHeaders.Rows)
            {
                if (!dr.IsNewRow && dr.Cells[0].Value != null)
                {
                    string colHeader = (string)dr.Cells[0].Value;
                    bool required = false;
                    if (dr.Cells[1].Value != null)
                        required = (bool)dr.Cells[1].Value;
                    colDetectorSettings.ColumnHeadersInFile.Add(colHeader);
                }
            }
            return colDetectorSettings;
        }

        private void DisplayColDectorSettings(TableColumnDetectorInstruction colDetectSettings)
        {
            this.dv_ColHeaders.Rows.Clear();
            if(colDetectSettings.ColumnHeadersInFile !=null && colDetectSettings.ColumnHeadersInFile.Count>0)
            {
                foreach(string colHeader in colDetectSettings.ColumnHeadersInFile)
                {
                    DataGridViewRow dr = new DataGridViewRow();
                    DataGridViewTextBoxCell cellColHeader = new DataGridViewTextBoxCell();
                    cellColHeader.Value = colHeader;
                    dr.Cells.Add(cellColHeader);
                    DataGridViewCheckBoxCell cellRequired = new DataGridViewCheckBoxCell();
                    bool required = false;
                    cellRequired.Value = required;
                    dr.Cells.Add(cellRequired);
                    this.dv_ColHeaders.Rows.Add(dr);
                }
            }
        }
        #endregion
    }
}
